
/*
	project:	LED Tester

    file:       main.c

    purpose:    main code file

    revisions
    000     RLL     2011-02-11  created file
    001     RLL     2011-02-25  switched to danni's LCD driver
    002     RLL     2011-03-12  changed from LCD to LED (7-seg)
                                for the display (4 digits xx.xx)

    notes
    For a multiplexed LED display, refresh rate needs to be at least 85Hz
    (1/85Hz = 11.8msec) to avoid any flickering of the digits. Since there
    are 4 digits, this means each digit must be rewritten every
    11.8/4 = 2.9msec.

    start with clock at 1 MHz

    For a digital meter display only need up to 5 measurements per second
    for a good display rate, which is a 200ms update rate for the display



*/

#include <string.h>

#include <stdlib.h>

#include "main.h"

#include "timers.h"

#include <util/delay.h>

// ---------------------------------------------------------

// global vars
uint8_t digitValue[5] = {0,0,0,0,0};      // value to be displayed in each digit
uint8_t currDigit = 0;                  // which display digit is currently "hot"?

// ---------------------------------------------------------

int main(void)
{
    // set up ports and pins
    io_init();

	// set up the timer
    timer_init();

    // enable global ints
    sei();

    // temporarily assign all 4 digit values to be 8's
    digitValue[1] = ONE;
    digitValue[2] = TWO;
    digitValue[3] = THREE;
    digitValue[4] = FOUR;

    while(1)
    {
        if (bit_is_set(sys_flags, sf_change_digit))
        {
            currDigit++;
            if (currDigit == 5)
                currDigit = 1;

            ShowDigit(currDigit);

            bit_clear(sys_flags, sf_change_digit);
        }
        //nop();
    }

}

// ---------------------------------------------------------

void ShowDigit( uint8_t currentDigit)
{
	// the currentDigit value is the index for the value in the
	// digitValue array, which will be put on the LED port

    // turn on the correct digit
    switch (currDigit)
    {
        case 1:     // digit 1 on
            LED_DIGIT_1 = ON;
            LED_DIGIT_2 = OFF;
            LED_DIGIT_3 = OFF;
            LED_DIGIT_4 = OFF;
            break;
        case 2:     // digit 2 on
            LED_DIGIT_1 = OFF;
            LED_DIGIT_2 = ON;
            LED_DIGIT_3 = OFF;
            LED_DIGIT_4 = OFF;
            break;
        case 3:     // digit 3 on
            LED_DIGIT_1 = OFF;
            LED_DIGIT_2 = OFF;
            LED_DIGIT_3 = ON;
            LED_DIGIT_4 = OFF;
            break;
        case 4:     // digit 4 on
            LED_DIGIT_1 = OFF;
            LED_DIGIT_2 = OFF;
            LED_DIGIT_3 = OFF;
            LED_DIGIT_4 = ON;
            break;
    }

    // send the correct vlaue to the port
    LED_PORT = digitValue[currDigit];
}

// ---------------------------------------------------------

void io_init()
{
    // set up the LED port
    // all pins as outputs, and all high (OFF)
    LED_DDR = 0xFF;
    LED_PORT = 0xFF;

    // set up the xtor pins
    // all 4 as outputs, and all four high (OFF)
    LED_DDR_DIGIT_1 = 1;
    LED_DDR_DIGIT_2 = 1;
    LED_DDR_DIGIT_3 = 1;
    LED_DDR_DIGIT_4 = 1;

    LED_DIGIT_1 = OFF;
    LED_DIGIT_2 = OFF;
    LED_DIGIT_3 = OFF;
    LED_DIGIT_4 = OFF;
}

// ---------------------------------------------------------

// ---------------------------------------------------------

// ---------------------------------------------------------
